package edu.nf.exam.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.entity.TaskInfo;
import edu.nf.exam.entity.UserInfo;
import edu.nf.exam.entity.UserTaskInfo;
import edu.nf.exam.service.TaskService;
import edu.nf.exam.service.UserTaskService;
import edu.nf.exam.service.impl.TaskServiceImpl;
import edu.nf.exam.service.impl.UserServiceImpl;
import edu.nf.exam.service.impl.UserTaskServiceImpl;
import org.nf.mvc.annotation.RequestMapping;
import org.nf.mvc.servlet.View;
import org.nf.mvc.view.JsonView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Author : LLH
 * @create 2021/7/13
 */
public class TaskController extends BaseController{
    /**
     * 添加
     * @param taskInfo
     * @return
     */
    @RequestMapping("/task/add")
    public View add(TaskInfo taskInfo){
        TaskService service=new TaskServiceImpl();
        try {
            // 添加任务信息，同时利用主键生成策略，返回任务编号
            service.add(taskInfo);
            Integer taskId = taskInfo.getTaskId();

            // 根据年级、身份查询学生
            UserInfo userInfo = new UserInfo();
            userInfo.setUserLevel(taskInfo.getLevelName());
            userInfo.setRole(1);
            List<UserInfo> list = new UserServiceImpl().select(userInfo);

            UserTaskService service2=new UserTaskServiceImpl();
            UserTaskInfo userTaskInfo = new UserTaskInfo();
            userTaskInfo.setTaskId(taskId);

            // 截取任务信息的试卷编号字段，得到一个试卷编号数组
            String[] examPaperIdArr = taskInfo.getPapers().split("&");

            // 循环学生对象集合，获取其学生ID，生成对应的用户任务
            for (UserInfo user : list) {
                // 嵌套循环试卷编号数组
                for (String s : examPaperIdArr) {
                    Integer examPaperId= Integer.valueOf(s);
                    userTaskInfo.setUserId(user.getUserId());
                    userTaskInfo.setExamPaperId(examPaperId);
                    service2.add(userTaskInfo);
                }

            }

            return new JsonView(success("提交成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 分页查询
     * @param title
     * @param levelName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/task/listTask")
    public View listTask(String title, String levelName, Integer page, Integer limit){
        PageInfo<TaskInfo> pageInfo = new TaskServiceImpl().listTask(title, levelName, page, limit);

        JsonView jsonView = new JsonView(page(pageInfo));
        jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return jsonView;
    }


    /**
     * 根据年级查询
     * @param
     * @return
     */
    @RequestMapping("/task/selectByLevelName")
    public View selectByLevelName(String levelName,Integer userId){
        // 根据年级查询出对应所有的任务信息
        List<TaskInfo> taskList = new TaskServiceImpl().selectByLevel(levelName);

        UserTaskService userTaskService=new UserTaskServiceImpl();
        // 循环遍历任务对象集合
        for (TaskInfo task : taskList) {
            // 根据用户编号和任务编号查询出当前用户的用户任务信息
            UserTaskInfo userTaskInfo=new UserTaskInfo();
            userTaskInfo.setUserId(userId);
            userTaskInfo.setTaskId(task.getTaskId());

            List<UserTaskInfo> userTaskInfoList = userTaskService.select(userTaskInfo);

            task.setUserTaskInfos(userTaskInfoList);
        }

        JsonView jsonView = new JsonView(success(taskList));
        jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return jsonView;
    }

    /**
     * 多条件查询
     * @param taskInfo
     * @return
     */
    @RequestMapping("/task/select")
    public View select(TaskInfo taskInfo){
        List<TaskInfo> list = new TaskServiceImpl().select(taskInfo);
        return new JsonView(success(list));
    }

}
